﻿using System;
using System.Collections.Generic;
using System.Text;
using GeneratorLibrary.ClassGeneratorLibrary;
using System.IO;
using System.Text.RegularExpressions;

namespace ClassGeneratorExpress {
    public class ProjectExpressInterface : IProjectInterface {
        string directory;
        string rootNamespace;

        #region IProjectInterface Members

        public string GetRootNameSpace(string path) {
            rootNamespace = "";
            getNamespace(new DirectoryInfo(path));
            return rootNamespace;
        }

        public string Directory {
            get { return directory; }
        }

        public void AddFromFile(List<string> path, string file) {
            FileInfo f = new FileInfo(file);
            string actualPath = directory;
            if (path.Count > 0) {
                actualPath += "\\" + String.Join("\\", path.ToArray());
            }
            if (f.DirectoryName == actualPath) {
                //Do nothing
            } else {
                f.MoveTo(actualPath + "\\" + f.Name);
            }
        }

        public void AddFolder(string NewFolder, List<string> path) {
            string actualPath = directory;
            if (path.Count > 0) {
                actualPath += "\\" + String.Join("\\", path.ToArray());
            }
            DirectoryInfo d = new DirectoryInfo(actualPath + "\\" + NewFolder);
            d.Create();
        }
        public void DeleteFile(List<string> path, string file) {
            string filepath = directory + "\\";
            if (path.Count > 0)
                filepath += String.Join("\\", path.ToArray()) + "\\";
            filepath += file;

            new FileInfo(filepath).Delete();
        }
        public void DeleteFolder(List<string> path, string folder) {
            string filepath = directory + "\\";
            if (path.Count > 0)
                filepath += String.Join("\\", path.ToArray()) + "\\";
            filepath += folder;
            new DirectoryInfo(filepath).Delete();
        }
        #endregion

        public ProjectExpressInterface(string directory) {
            this.directory = directory;
        }

        private bool getNamespace(DirectoryInfo dir) {
            foreach (FileInfo file in dir.GetFiles("*.cs")) {
                if (file.Name.EndsWith(".aspx.cs"))
                    continue;
                using (StreamReader r = new StreamReader(file.FullName)) {
                    string str = r.ReadToEnd();
                    Regex regex = new Regex("namespace\\s+(\\S+)\\s*{");
                    Match m = regex.Match(str);
                    if (m != null) {
                        rootNamespace = m.Groups[0].Value.Split(new char[] { '.' })[0];
                        return true;
                    }
                }
            }
            if (dir.Name.Contains(this.directory) && getNamespace(dir.Parent))
                return true;
            return false;
        }
        public bool AllowOverrideNamespace {
            get {
                return true;
            }
        }
    }
}
